package com.classic.adapter;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.classic.base.BaseFragment;
import com.classic.fragment.EpisodeFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListAdapter extends FragmentPagerAdapter {
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<String> strings = new ArrayList<>();

    public ListAdapter(FragmentManager childFragmentManager) {
        super(childFragmentManager);
        initData(null);
    }

    void initData(List<String> strings) {
        if (null == strings)
            strings = Arrays.asList("经典段子", "猪", "老婆", "媳妇", "小明", "女人", "女生", "单身", "姑娘",
                    "老板", "女朋友", "男人", "工资", "医生", "朋友", "二胎", "飞机", "对联", "世界杯", "闺蜜",
                    "厕所", "毕业", "理发", "卫生纸", "双十一", "山东");
        this.strings = strings;
        for (int i = 0; i < strings.size(); i++) {
            String s;
            if (i == 0) {
                s = "page";
            } else {
                s = "tag/" + strings.get(i);
            }
            fragments.add(getEAFragment(s));
        }
    }

    public ListAdapter(FragmentManager c, List<String> strings) {
        super(c);
        initData(strings);
    }

    private BaseFragment getEAFragment(String i) {
        EpisodeFragment fragment = new EpisodeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EpisodeFragment.DATA_, i);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }

    public void setFragments(List<BaseFragment> fragments) {
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }


    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return strings.get(position);
    }
}
